package com.xiebishe.mylibrary.net.retrofit;


import com.xiebishe.mylibrary.base.MyApplication;
import com.xiebishe.mylibrary.net.okhttp.OkHttpClientManager;
import com.xiebishe.mylibrary.net.retrofit.fastjsonConverter.FastJsonConverterFactory;
import com.xiebishe.mylibrary.net.retrofit.remoteCallAdapter.RemoteCallFactory;
import com.xiebishe.mylibrary.net.retrofit.rxjavaCallAdapter.MyRxJava2CallAdapterFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import retrofit2.Retrofit;


/**
 * @class RepositoryManager
 * @describe 管理数据接口，net+db
 */
public class RepositoryManager implements IRepositoryManager {

    /**
     * 单例
     */
    private static volatile RepositoryManager instance;
    private RemoteCallFactory remoteCallFactory;
    private FastJsonConverterFactory converterFactory;

    public static RepositoryManager getInstance() {
        if (instance == null) {
            synchronized (RepositoryManager.class) {
                if (instance == null) {
                    instance = new RepositoryManager();
                }
            }
        }
        return instance;
    }

    private RepositoryManager() {
        createRetrofit();
    }


    private Retrofit mNormnalRetrofit;

    private final Map<String, Object> mRetrofitServiceCache = new ConcurrentHashMap<>();
    private final Map<String, Retrofit> mRetrofitCache = new ConcurrentHashMap<>();
    private MyRxJava2CallAdapterFactory rxJavaCallAdapterFactory;

    private void createRetrofit() {
        rxJavaCallAdapterFactory = MyRxJava2CallAdapterFactory.create();
        remoteCallFactory = RemoteCallFactory.create();
        converterFactory = FastJsonConverterFactory.create();
        mNormnalRetrofit = new Retrofit.Builder()
                .baseUrl(MyApplication.getInstance().getHost())               //主域  .baseUrl(Constant.Net.DOMAIN_MARBLE)
                .client(OkHttpClientManager.getInstance().getOkHttpClient())
                .addCallAdapterFactory(remoteCallFactory)
                .addCallAdapterFactory(rxJavaCallAdapterFactory)
                .addConverterFactory(converterFactory)
                .build();
    }


    @Override
    public <T> T obtainRetrofitService(Class<T> service) {
        String serviceClassName = service.getCanonicalName();
        T retrofitService = (T) mRetrofitServiceCache.get(serviceClassName);
        if (retrofitService == null) {
            retrofitService = createService(service);
            mRetrofitServiceCache.put(service.getCanonicalName(), retrofitService);
        }
        return retrofitService;
    }

    private <T> T createService(Class<T> service) {
        return mNormnalRetrofit.create(service);
    }

    private Retrofit getRetrofit(String baseUrl) {
        Retrofit retrofit = mRetrofitCache.get(baseUrl);
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .client(OkHttpClientManager.getInstance().getOkHttpClient())
                    .addCallAdapterFactory(remoteCallFactory)
                    .addConverterFactory(converterFactory)
                    .build();
            mRetrofitCache.put(baseUrl, retrofit);
        }
        return retrofit;

    }


    public static void removeInstance() {
        if (instance != null) {
            instance = null;
        }
    }
}
